/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * 深度优先遍历
 * @param {TreeNode} root
 * @return {number[]}
 */
//  var rightSideView = function (root) {
//   let res = []
//   function dfs(node,level){
//     if(!node) return
//     res[level] = node.val

//     if(node.left) dfs(node.left,level+1)
//     if(node.right) dfs(node.right,level+1)
//   }
//   dfs(root,0)
//   return res


// };

/**
 * 广度优先遍历
 * @param {TreeNode} root
 * @return {number[]}
 */
 var rightSideView = function (root) {
  let queue = []
  if(!root) return queue
  queue.push(root)
  let res = []
  while(queue.length){
    let len = queue.length
    res.push(queue[len-1].val)
    for(let i=0;i<len;i++){
      let node = queue.shift()
      if(node.left) queue.push(node.left)
      if(node.right) queue.push(node.right)
    }
  }
  return res


};